package com.ki.glasscase.svc.services.impl;

import com.ki.glasscase.svc.dto.ProductDTO;
import org.springframework.stereotype.Component;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Khacheresyants.L on 2/17/14.
 */
@Component
public class ProductHandler extends DefaultHandler {
    private List<ProductDTO> productDTOs = null;
    private ProductDTO product = null;
    private HashMap<String,String> categories= null;
    private String key = null;
    private String value = null;

    public List<ProductDTO> getProductDTOs() {
        return productDTOs;
    }

    private boolean name = false;
    private boolean vendor = false;
    private boolean description = false;
    private boolean price = false;
    private boolean oldPrice = false;
    private boolean shopName = false;
    private boolean imgUrl = false;
    private boolean buyUrl = false;
    private boolean tags = false;
    private boolean categoryId = false;
    private boolean param = false;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("offer")) {
            product = new ProductDTO();
            String original_id = attributes.getValue("original_id");
            product.setOriginalId(original_id);
            String available = attributes.getValue("available");
            product.setAvailable(Boolean.parseBoolean(available));

            if (productDTOs == null) {
                productDTOs = new ArrayList<ProductDTO>();
            }

        } else if( qName.equalsIgnoreCase("category")){

            key = attributes.getValue("id");
            if(categories == null) {
                categories = new HashMap<String, String>();
            }

            tags = true;
        }
        else if (qName.equalsIgnoreCase("description")) {
            description = true;
        } else if (qName.equalsIgnoreCase("picture")) {
            imgUrl = true;
//        } else if (qName.equalsIgnoreCase("param")) {
//            param = true;
        } else if (qName.equalsIgnoreCase("categoryId")) {
            categoryId = true;
        } else if (qName.equalsIgnoreCase("vendor")) {
            vendor = true;
        } else if (qName.equalsIgnoreCase("oldprice")) {
            oldPrice = true;
        } else if (qName.equalsIgnoreCase("price")) {
            price = true;
        } else if (qName.equalsIgnoreCase("advcampaign_name")) {
            shopName = true;
        } else if (qName.equalsIgnoreCase("url")) {
            buyUrl = true;
        } else if (qName.equalsIgnoreCase("name")) {
            name = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(qName.equalsIgnoreCase("category")) {
            categories.put(key,value);
        }
        if (qName.equalsIgnoreCase("offer")) {
            productDTOs.add(product);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

        if (description) {
            product.setDescription(new String(ch, start, length));
            description = false;
        } else if(tags) {
            value = new String(ch, start, length);
            categories.put(key,value);
            tags = false;
        }
        else if (imgUrl) {
            product.setImgUrl(new String(ch, start, length));
            imgUrl = false;
//        } else if (param) {
//            product.getTags().add(new String(ch, start, length));
//            param = false;
        } else if (categoryId) {
            String result = new String(ch, start, length);
            if(categories.containsKey(result)){
                result = categories.get(result);
            }
            product.getTags().add(result);
            categoryId = false;
        } else if (vendor) {
            product.getTags().add(new String(ch, start, length));
            vendor = false;
        } else if (oldPrice) {
            product.setOldPrice(Double.parseDouble(new String(ch, start, length)));
            oldPrice = false;
        } else if (price) {
            product.setPrice(Double.parseDouble(new String(ch, start, length)));
            price = false;
        } else if (shopName) {
            product.setShopName(new String(ch, start, length));
            shopName = false;
        } else if (buyUrl) {
            product.setBuyUrl(new String(ch, start, length));
            buyUrl = false;
        } else if (name) {
            product.setName(new String(ch, start, length));
            name = false;
        }
    }

}
